﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DBLOG.BizTalk.BizTalkExplorerServices.Core;
using DBLOG.BizTalk.BizTalkExplorerServices.Entities;
using Microsoft.BizTalk.ExplorerOM;

namespace DBLOG.BizTalk.BizTalkExplorerServices.Explorers
{
    public class WarningExplorer : BizTalkExplorerBase
    {
        private List<BTSWarning> _warnings { get; set; }

        public List<BTSWarning> GetWarnings()
        {
            _warnings = new List<BTSWarning>();

            this.GetOrchestrationWarnings();

            this.GetSendPortWarnings();

            this.GetReceiveLocationWarnings();

            return _warnings;
        }

        private void GetOrchestrationWarnings()
        {
            using (OrchestrationExplorer orchestrationExplorer = new OrchestrationExplorer())
            {
                List<BtsOrchestration> disabledOrchestrations = orchestrationExplorer.GetDisabledOrchestrations();

                foreach (BtsOrchestration disabledOrchestration in disabledOrchestrations)
                {
                    _warnings.Add(new BTSWarning()
                    {
                        Name = "Orchestration disabled",
                        ApplicationName = disabledOrchestration.Application.Name,
                        Date = DateTime.Now,
                        BizTalkWarningType = WarningType.Orchestration,
                        Details = String.Format("The orchestration {0} is disabled.", disabledOrchestration.FullName)
                    });
                }
            }
        }

        private void GetSendPortWarnings()
        {
            using (SendPortExplorer sendPortExplorer = new SendPortExplorer())
            {
                List<SendPort> disabledSendPorts = sendPortExplorer.GetDisabledSendPorts();
              
                foreach (SendPort disabledSendPort in disabledSendPorts)
                {
                    _warnings.Add(new BTSWarning()
                    {
                        Name="Send port disabled",
                        ApplicationName = disabledSendPort.Application.Name,
                        Date = DateTime.Now,
                        BizTalkWarningType = WarningType.SendPort,
                        Details = String.Format("The send port {0} is disabled", disabledSendPort.Name)
                    });
                }
            }
        }

        private void GetReceiveLocationWarnings()
        {
            using (ReceiveLocationExplorer receiveLocationExplorer = new ReceiveLocationExplorer())
            {
                List<ReceiveLocation> receiveLocations = receiveLocationExplorer.GetDisabledReceiveLocations();

                foreach (ReceiveLocation receiveLocation in receiveLocations)
                {
                    _warnings.Add(new BTSWarning()
                    {
                        Name = "Receive location disabled",
                        ApplicationName = receiveLocation.ReceivePort.Application.Name,
                        Date = DateTime.Now,
                        BizTalkWarningType = WarningType.ReceiveLocation,
                        Details = String.Format("The receive location {0} is disabled", receiveLocation.Name)
                    });
                }
            }
        }
    }
}